<?php

	class wh_category_model extends st_model {
		protected $_instance  = array(
										  'table'      => 'wh_category', 
										  'primaryKey' => 'id',
									  );
									  
		public function getCategoryUrl() 
		{
			return site_url('admin/category/index/id/' . $this->getId(), true);
		}
		
		public function getIndexUrl($param = '') 
		{
			return site_url('admin/category/index/' . $param, true);
		}
		
		public function hasChildren()
		{
			return (count($this->getChildren()) > 0);
		}
		
		public function getChildren() 
		{
			if (!$this->isLoaded()) {
				return null;
			}
			$model = new wh_category_model();
			$model->addAttributeToFilter('parent_id', $this->getId());
			return $model->getCollection(); 	
		}
		
		public function getParent()
		{
			if (!$this->isLoaded() || $this->getParentId() === '0') {
				return null;
			}
			$parent = new category_model();
			$parent->loadByPK($this->getParentId());
			return $parent;
		}
		
		public function isSelf($id) {
			if (!$this->isLoaded()) {
				return false;
			}
			if ($this->getId() == $id) {
				return true;
			}
			return false;
		}
		
		public function isParent($parent_id)
		{
			if (!$this->isLoaded()) {
				return false;
			}
			if ($this->getParentId() == $parent_id) {
				return true;				
			}
			return false;
		}
		
		public function isAccessor($parent_id) 
		{
			$valid = false;
			if (!$this->isLoaded()) {
				return $valid;
			}
			if ($this->getParentId() == $parent_id) {
				$valid = true;
			} else {
				$parent = $this->getParent();
				if ($parent != null && $parent->isLoaded()) {
					$valid = $parent->isAccessor($parent_id);
				}
			}
			return $valid;
		}
		
		public function genTree($parent_id = 0, $current_id = 0, $show_id = true)
		{
			$output = "";
			$this->resetFilteredList();
			$this->addAttributeToFilter('parent_id', $parent_id);
			$collection = $this->getCollection();
			if ($collection && count($collection) > 0) {
				if ($parent_id != 0) {
					$output .= "<ul>\n";
				}
				foreach ($collection as $category)
				{
					$output .= "<li";
					$output .= " class=\"";
					if ($category->getIsActive() == 0) {
						$output .= " disabled";
					}
					if ($current_id == $category->getId()) {
						$output .= " currentCat";
					}
					$output .= "\"";
					$output .= ' onclick="window.location="'.$category->getCategoryUrl().'""';
					$output .= ">\n";
					$output .= "<a href=\"".$category->getCategoryUrl()."\">\n";
					$output .= "<span>\n";
					if ($show_id) {
						$output .= "<strong>(ID: ".$category->getId(). ')</strong>&nbsp;';
					}
					$output .= $category->getName()."\n";
					$output .= "</span>\n";
					$output .= "</a>\n";
					$output .= $category->genTree($category->getId());
					$output .= "</li>\n";
				}
				if ($parent_id != 0) {
					$output .= "</ul>\n";
				}				
			}
			return $output;				
		}
	}	
?>